package it.eng.area118.mdogpscollector;

import it.eng.area118.mdocommon.android.notification.AndroidNotificationManager;
import it.eng.area118.mdocommon.android.util.AndroidApplicationContext;
import it.eng.area118.mdocommon.notification.InputTextDialogCallback;
import it.eng.area118.mdogpscollector.model.CheckPoint;
import it.eng.area118.mdogpscollector.util.ViewUtils;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class InsertCheckPointFragment extends Fragment {

	private static String[] contentType = new String[] { "AUTOSTRADA" };
	private TextView type;
	private TextView name;
	private TextView km;
	private View picker;
	private TextView course;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.insert_checkpoint_layout, container, false);
		initInsertTypeChoice(rootView);
		initSaveButton(rootView);
		initInsertNameChoice(rootView);
		initInsertKmChoice(rootView);
		initInsertDirection(rootView);
		return rootView;
	}

	private void initInsertDirection(View rootView) {
		course = ViewUtils.getView(rootView, R.id.save_field_checkPointCourse);
		View courseContainer = (View) ViewUtils.getView(rootView, R.id.save_field_checkPointCourse_container);
		if (courseContainer != null) {
			courseContainer.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					AndroidNotificationManager.getInstance().showOptionDialog("Scegliere Direzione Punto", "Scegliere una delle seguenti opzioni", new String[] { "NORD", "SUD", "OVEST", "EST" }, new InputTextDialogCallback() {

						@Override
						public void onDataReceived(String data) {
							course.setText(data);
						}
					});

				}
			});
		}

	}

	private void initInsertKmChoice(View rootView) {
		km = ViewUtils.getView(rootView, R.id.save_field_checkPointKm);

		View kmContainer = (View) ViewUtils.getView(rootView, R.id.save_field_checkPointKm_container);

		picker = rootView.findViewById(R.id.number_picker);
		if (picker == null) {
			picker = it.eng.area118.mdocommon.android.util.ViewUtils.inflate(AndroidApplicationContext.getInstance().getApplicationContext(), R.layout.number_picker_layout);
		}
		if (kmContainer != null) {
			kmContainer.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int initValue = 0;
					try {
						Integer.valueOf(km.getText().toString());
					} catch (Exception e) {
						Log.e("", "error", e);
						initValue = -1;
					}
					int[] btExtra = new int[] { R.id.btPickerPlus, R.id.btPickerMinus };

					AndroidNotificationManager.getInstance().showNumberPickerDialog("Kilometro", "Inserire il km di ricerca(intervallo aperto)", picker, R.id.number_picker, R.layout.number_picker_layout, btExtra, initValue, 50, new InputTextDialogCallback() {

						@Override
						public void onDataReceived(String data) {
							km.setText(data);
						}
					});
				}
			});
		}

	}

	private void initSaveButton(View rootView) {

		Button bt = (Button) rootView.findViewById(R.id.btsaveCheckPoints);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CheckPoint cp = new CheckPoint();
				cp.setType("" + type.getText());
				cp.setName("" + name.getText());
				cp.setPoint("" + km.getText());
				cp.setDirection("" + course.getText());
				Log.i("initSaveButton", "Saving new check point: " + cp.getItemKey());
			}
		});

	}

	private void initInsertNameChoice(View rootView) {
		View v = rootView.findViewById(R.id.save_field_checkPointDesc_container);
		name = (TextView) rootView.findViewById(R.id.save_field_checkPointDesc);
		v.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AndroidNotificationManager.getInstance().showInputTextDialog("Inserire nome del punto", "Inserire nome del punto", new InputTextDialogCallback() {

					@Override
					public void onDataReceived(String data) {
						name.setText(data);

					}
				});
			}
		});

	}

	private void initInsertTypeChoice(View rootView) {
		View v = rootView.findViewById(R.id.save_field_checkPointType_container);
		type = (TextView) rootView.findViewById(R.id.save_field_checkPointType);
		v.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AndroidNotificationManager.getInstance().showOptionDialog("Scegliere Tipologia Punto", "Scegliere una delle seguenti opzioni", contentType, new InputTextDialogCallback() {
					@Override
					public void onDataReceived(String data) {
						type.setText(data);
					}
				});
			}
		});

	}
}
